package com.china08.yxyapi.api.home.activity.marker;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.maker.YxyMakerWorks;
import com.china08.yxyapi.repository.db.marker.YxyMakerWorksRepository;

/**
 * 获取创客作品查看接口
 */
@CrossOrigin
@RestController
public class YxyMakerPreViewController extends YxyApiBaseController {

    @Value("${yxy.res.url}")
    private String yxy_res_url;

    @Autowired
    private YxyMakerWorksRepository yxyMakerWorksRepository;

    @RequestMapping(value = "/api_yw/home/marker/works/preview/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response,
            @PathVariable(value = "id") String id) {
        RespModel model = new RespModel();
        YxyMakerWorks work = yxyMakerWorksRepository.findOne(id);
        // 作品名称
        String name = work.getName();
        // 报名文件预览url
        String viewurl = yxy_res_url + "/res/browseHrbMaker/" + work.getId() + "?registerFileLocation="
                + work.getRegisterFile();
        // 作品压缩包名称
        String workfilename = work.getWorksFileName();
        // 作品压缩包下载url
        String zipurl = yxy_res_url + "/res/downloadHrbMaker/worksFile/" + work.getId() + "?downloadFilename="
                + work.getWorksFileName() + "&worksFilename=" + work.getWorksFileName() + "&worksFileLocation="
                + work.getWorksFile();
        model.title = name;
        model.preview = viewurl;
        model.workname = workfilename;
        model.downloadLocation = zipurl;
        return model;
    }

    protected static class RespModel {
        // 报名名称
        public String title;
        // 报名表预览url
        public String preview;
        // 压缩包名称
        public String workname;
        // 压缩包下载url
        public String downloadLocation;
    }

}
